package com.bsi.rd.framework.auth.controller;
/*
 * Author: liusongs
 * Date: 2019/10/16 17:14
 * Content: 测试fegin调用
 */


import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Service;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/fegin")
public class FeginController {
    @Autowired
    private FeginInterFace feginInterFace;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "test1")
    public String test1(HttpServletRequest request) {
        ServiceInstance gateway = loadBalancerClient.choose("gateway");
        StringBuffer append = new StringBuffer().append(gateway.getUri()).append("/demo/get");
        HttpHeaders httpHeaders = new HttpHeaders();
        if (request.getHeader("Content-type") == null) {
            httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        }
        String body = restTemplate.exchange(append.toString(), HttpMethod.POST,
                new HttpEntity<String>(null, httpHeaders), String.class).getBody();

        //return feginInterFace.test1();
        return body;
    }
}


